package com.zidian.teacher.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zidian.teacher.R;
import com.zidian.teacher.adapter.StudentEvaAdapter;
import com.zidian.teacher.bean.StudentEvaBean;
import com.zidian.teacher.common.Constant;
import com.zidian.teacher.utils.GetUserInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by GongCheng on 2017/1/9.
 */

public class StudentEvaFragment extends Fragment {
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private StudentEvaAdapter adapter;

    public static StudentEvaFragment newInstance() {

        Bundle args = new Bundle();

        StudentEvaFragment fragment = new StudentEvaFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_student_eva, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void initView() {
        adapter = new StudentEvaAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);
        getStudentEvaluate();
    }

    private void getStudentEvaluate() {
        String url = Constant.URL_TEACHER + "EvaluateByStudent/studentEvaluation";
        Map<String, String> map = new HashMap<>();
        map.put("teacherId", GetUserInfo.getTeacherId(getContext()));
        map.put("token", GetUserInfo.getToken(getContext()));
        OkHttpUtils.post().params(map).url(url).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (recyclerView != null) {
                    Snackbar.make(recyclerView, "加载失败", Snackbar.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getInt("code") != 200) {
                        Snackbar.make(recyclerView, jsonObject.getString("message"), Snackbar.LENGTH_SHORT).show();
                    } else {
                        Gson gson = new Gson();
                        StudentEvaBean bean = gson.fromJson(response, StudentEvaBean.class);
                        adapter.setData(bean);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
